import time

from pdbc_p.pdbc import db_queryall, db_queryone, db_operation


from utinity_p.utinity import check_number


def  browse_book():
    # 二维元组
    result=db_queryall("select book_id,bookname,author,press,sales_price,amount from libary ")
    for item in result:
        for i in item:
            print(i,end="\t")
    bookname=input("输入你要查询的书名：")
#     判断书本是否存在
    r=db_queryone("select book_id,bookname,author,press,sales_price,amount from libary where bookname='%s'"%bookname)
    if r!=None:
        for item in r:
            print(item, end="\t")
        print("")
        q1=input("是否需要加购物车：1：是；任意键：返回用户操作菜单")
        if q1=='1':
            amount=input("请输入购买数量：")
            if check_number(amount):
                if r[5]>=int(amount):
                    total_money=r[4]*int(amount)
                    from user_p.user import username
                    db_operation("insert into buycar(username,bookid,amount,total_money) values ('%s',%d,%d,%f)"%
                                 (username,r[0],int(amount),total_money))
                    print("成功加入购物车")
        from ui_p.ui import user_menu
        user_menu()

def payment():
    from user_p.user import username
    result1=db_queryall("select * from buycar where username='%s' and order_status='on'"%username)
    for item in result1:
        for i in item:
            print(i,end="\t")
        print("")
    q2=input("是否需要现在结算：1：是；任意键：返回主菜单")
    # 结算：（（"小团圆"，100），（"程序员"，500））
    if q2=="1":
        result=db_queryone("select * from user_info where username='%s'"%username)
        if result[2]==None or result[3]==None or result[4]==None:
            print("请完善用户信息：")
        else:
            total_pay=0
            for item in result1:
                total_pay+=item[5]
            print("你共需支付%.2f"%total_pay)
            q3=input("你有一张8.8折的优惠券，是否使用？1：是；任意键：否：")
            if q3=="1":
                total_pay*=0.88
                print("你共需支付%.2f" % total_pay)
                time.sleep(1)
                print("支付完成，请耐心等待你的宝贝！")
            else:
                print("支付完成，请耐心等待你的宝贝！")
    #             修改购物车订单状态；库存减少
            db_operation("update buycar set order_status='off' where username='%s'"%username)
            for item in result1:
                db_operation("update libary set amount=amount-%d where bookid=%d"%(item[6],item[0]))
    else:
        from ui_p.ui import user_menu
        user_menu()
























